package com.hspedu.com.jdbc.utils;

import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;

/**
 * @author DL5O
 * @version 1.0
 */
public class JDBCUtilsByDruid_USE {

    @Test
    //测试查询语句
    public void testSelect() {
        //1.得到连接
        Connection connection = null;
        //2.组织修改语句
        String sql = "select * from actor where id >= ?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            System.out.println(connection.getClass());//class com.alibaba.druid.pool.DruidPooledConnection
            preparedStatement = connection.prepareStatement(sql);
            //给占位符赋值
            preparedStatement.setInt(1, 3);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");//getName()
                String sex = resultSet.getString("sex");//getSex()
                Date borndate = resultSet.getDate("borndate");
                String phone = resultSet.getString("phone");
                System.out.println(id + "\t" + name + "\t"
                        + sex + "\t" + borndate + "\t" + phone);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            JDBCUtilsByDruid.close(resultSet, preparedStatement, connection);
        }
    }

    @Test
    //使用土方法来解决ResultSet =封装=> 封装到ArrayList
    public ArrayList<Actor> testSelectToArrayList() {
        //1.得到连接
        Connection connection = null;
        //2.组织修改语句
        String sql = "select * from actor where id >= ?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ArrayList<Actor> actors = new ArrayList<>();//用于存放Actor对象
        try {
            connection = JDBCUtilsByDruid.getConnection();
            //System.out.println(connection.getClass());//class com.alibaba.druid.pool.DruidPooledConnection
            preparedStatement = connection.prepareStatement(sql);
            //给占位符赋值
            preparedStatement.setInt(1, 3);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");//getName()
                String sex = resultSet.getString("sex");//getSex()
                Date borndate = resultSet.getDate("borndate");
                String phone = resultSet.getString("phone");
                //把得到的该条记录封装到一个actor对象，然后在放入到list集合中
                actors.add(new Actor(id, name, sex, borndate, phone));
            }
            System.out.println("actors集合数据=" + actors);
            for (Actor actor : actors) {
                System.out.println();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            JDBCUtilsByDruid.close(resultSet, preparedStatement, connection);
        }
        //因为ArrayList 和 Connection 没有任何关联，所以集合可以复用
        return actors;
    }
}
